{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Mzt6JmpwLnl0",
        "outputId": "bb600d41-0b2f-4c7e-ab47-0e645277988b"
      },
      "outputs": [],
      "source": [
        "!pip install pytrends"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "9S9Szn46Lp65"
      },
      "outputs": [],
      "source": [
        "from pytrends.request import TrendReq\n",
        "import seaborn\n",
        "# for styling\n",
        "seaborn.set_style(\"darkgrid\")\n",
        "\n",
        "# initialize a new Google Trends Request Object\n",
        "pt = TrendReq(hl=\"en-US\", tz=360)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "_3hktMxsLq4M"
      },
      "outputs": [],
      "source": [
        "# set the keyword & timeframe\n",
        "pt.build_payload([\"Python\", \"Java\"], timeframe=\"all\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 455
        },
        "id": "5bj7PEPZLruO",
        "outputId": "94d313cb-c106-43e8-921f-edc7b7a82ec3"
      },
      "outputs": [],
      "source": [
        "# get the interest over time\n",
        "iot = pt.interest_over_time()\n",
        "iot"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 406
        },
        "id": "BG8uQd3zLsw-",
        "outputId": "1f9d7327-bad5-4d05-8b1d-046c7527864c"
      },
      "outputs": [],
      "source": [
        "# plot it\n",
        "iot.plot(figsize=(10, 6))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 455
        },
        "id": "65qkNWguL8g-",
        "outputId": "43332253-4215-48f7-e25a-c4dcc2a9f99a"
      },
      "outputs": [],
      "source": [
        "# get hourly historical interest\n",
        "data = pt.get_historical_interest(\n",
        "    [\"data science\"], \n",
        "    cat=396, \n",
        "    year_start=2022, month_start=1, day_start=1, hour_start=0,\n",
        "    year_end=2022, month_end=2, day_end=10, hour_end=23,\n",
        ")\n",
        "data"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "NYoPNiSiVWFo"
      },
      "outputs": [],
      "source": [
        "# the keyword to extract data\n",
        "kw = \"python\"\n",
        "pt.build_payload([kw], timeframe=\"all\")\n",
        "# get the interest by country\n",
        "ibr = pt.interest_by_region(\"COUNTRY\", inc_low_vol=True, inc_geo_code=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "bjWBRVS-XNfo",
        "outputId": "db776b26-2487-4ba1-d096-9c84dec93d1f"
      },
      "outputs": [],
      "source": [
        "# sort the countries by interest\n",
        "ibr[kw].sort_values(ascending=False)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 833
        },
        "id": "ny8R1DKBXON4",
        "outputId": "8abbf1af-a186-42ea-91a2-56c5bd165f7d"
      },
      "outputs": [],
      "source": [
        "# get related topics of the keyword\n",
        "rt = pt.related_topics()\n",
        "rt[kw][\"top\"]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 833
        },
        "id": "dCXqM2uBXv7M",
        "outputId": "686c8144-50f5-445c-929f-dfda1494a38b"
      },
      "outputs": [],
      "source": [
        "# get related queries to previous keyword\n",
        "rq = pt.related_queries()\n",
        "rq[kw][\"top\"]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "FYCQ-ejxZv40",
        "outputId": "2ee09da5-6d19-4ba1-c44a-5dd15a683387"
      },
      "outputs": [],
      "source": [
        "# get suggested searches\n",
        "pt.suggestions(\"python\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jFvUTnBBaYTS",
        "outputId": "95ca6d7d-f801-467d-ade1-e2d5c009bcf5"
      },
      "outputs": [],
      "source": [
        "# another example of suggested searches\n",
        "pt.suggestions(\"America\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 676
        },
        "id": "AEKb0IY0YLSx",
        "outputId": "8c4519ba-143b-48e2-fc87-b5c5d9a7d56f"
      },
      "outputs": [],
      "source": [
        "# trending searches per region\n",
        "ts = pt.trending_searches(pn=\"united_kingdom\")\n",
        "ts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 423
        },
        "id": "vFDybnL-YaiF",
        "outputId": "c826398b-6a1f-4bc2-f5b8-1c1b8ea8f0f8"
      },
      "outputs": [],
      "source": [
        "# real-time trending searches\n",
        "pt.realtime_trending_searches()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [],
      "name": "Extracting-GoogleTrends-Data_PythonCodeTutorial.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
